#include<stdio.h>
#include<unistd.h>
#include<pthread.h>


int bowl=0;
pthread_mutex_t lock;
pthread_cond_t eatcond;
pthread_cond_t makecond;


void* pthread_eat(void* arg){
    pthread_detach(pthread_self());
    while(1){
    pthread_mutex_lock(&lock);
    while(bowl<=0){
    pthread_cond_wait(&eatcond,&lock);
        }
    printf("i am eating :%d \n",bowl);
    bowl--;
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&makecond);
    }
}
void* pthread_make(void* arg){
    pthread_detach(pthread_self());
    while(1){
    pthread_mutex_lock(&lock);
    while(bowl>0){
    pthread_cond_wait(&makecond,&lock);
        }
    bowl++;
    printf("i am making :%d \n",bowl);
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&eatcond);
    }
}
int main(){
    pthread_mutex_init(&lock,NULL);
    pthread_cond_init(&eatcond,NULL);
    pthread_cond_init(&makecond,NULL);
    pthread_t tid;
   int i=0;
   for(i;i<2;i++){
    int ret=pthread_create(&tid,NULL,pthread_eat,NULL);
    if(ret<0){
        perror("pthread_create");
        return 0;
    }
    ret=pthread_create(&tid,NULL,pthread_make,NULL);
    if(ret<0){
        perror("pthread_create");
        return 0;
    }
   }
   while(1){
       sleep(1);
   }
   pthread_mutex_destroy(&lock);
    pthread_cond_destroy(&eatcond);
    pthread_cond_destroy(&makecond);
    return 0;
}
